<?php
namespace Models;

require_once('\Common\DBTables.php');

class Install
{
	private $_dbt;
	
	public function __construct()
	{
		$this->_dbt = new \Common\DBTables();
	}
	
	public function installed($host, $db, $dbUser, $dbPassword)
	{
		$tables = array($this->_dbt->albumTable, $this->_dbt->imageTable, $this->_dbt->userTable, $this->_dbt->tokenTable, $this->_dbt->userTypeTable);
		$connection = mysql_connect($host, $dbUser, $dbPassword);
		mysql_select_db($db, $connection);
		
		foreach ($tables as $table)
		{
			$sql= "DESC ".$table.";";
	        mysql_query($sql, $connection);
	        
			// Om någon tabell inte existerar så behöver applikationen ominstalleras
	        if(mysql_errno() == 1146)
	            return false;
		}
		return true;
	}
	
	public function install($host, $db, $dbUser, $dbPassword, $charset, $userName, $firstName, $lastName, $hashedPassword, $email)
	{		
		$connection = mysql_connect($host, $dbUser, $dbPassword);
		
		$args = func_get_args();
		foreach ($args as $arg)
		{
			// Returnera om någon parameter är tom
			if($arg == "")
				return false;
		}
		
		if (!$connection)
			return false;
		
		if(!mysql_select_db($db, $connection))
			return false;
		
		$sqlAlbum = "CREATE TABLE IF NOT EXISTS `".$this->_dbt->albumTable."` (
		  `".$this->_dbt->albumIdField."` int(11) NOT NULL AUTO_INCREMENT,
		  `".$this->_dbt->userIdField."` int(11) NOT NULL,
		  `".$this->_dbt->albumTitleField."` varchar(25) NOT NULL,
		  `".$this->_dbt->albumYearField."` year(4) NOT NULL,
		  `".$this->_dbt->albumDescriptionField."` varchar(250) NOT NULL,
		  PRIMARY KEY (`".$this->_dbt->albumIdField."`),
		  KEY `".$this->_dbt->userIdField."` (`".$this->_dbt->userIdField."`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		
		$sqlImage = "CREATE TABLE IF NOT EXISTS `".$this->_dbt->imageTable."` (
		  `".$this->_dbt->imageIdField."` int(11) NOT NULL AUTO_INCREMENT,
		  `".$this->_dbt->albumIdField."` int(11) NOT NULL,
		  `".$this->_dbt->imageDescriptionField."` varchar(100) NOT NULL,
		  `".$this->_dbt->imageUrlField."` varchar(250) NOT NULL,
		  `".$this->_dbt->thumbUrlField."` varchar(250) NOT NULL,
		  PRIMARY KEY (`".$this->_dbt->imageIdField."`),
		  KEY `".$this->_dbt->albumIdField."` (`".$this->_dbt->albumIdField."`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		
		$sqlToken ="CREATE TABLE IF NOT EXISTS `".$this->_dbt->tokenField."` (
		  `".$this->_dbt->tokenIdField."` int(11) NOT NULL AUTO_INCREMENT,
		  `".$this->_dbt->userIdField."` int(11) NOT NULL,
		  `".$this->_dbt->tokenField."` varchar(45) NOT NULL,
		  `".$this->_dbt->ipField."` varchar(15) NOT NULL,
		  PRIMARY KEY (`".$this->_dbt->tokenIdField."`),
		  KEY `".$this->_dbt->userIdField."` (`".$this->_dbt->userIdField."`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
		
		$sqlUser = "CREATE TABLE IF NOT EXISTS `".$this->_dbt->userTable."` (
		  `".$this->_dbt->userIdField."` int(11) NOT NULL AUTO_INCREMENT,
		  `".$this->_dbt->userTypeIdField."` int(11) NOT NULL,
		  `".$this->_dbt->userNameField."` varchar(15) NOT NULL,
		  `".$this->_dbt->firstNameField."` varchar(20) NOT NULL,
		  `".$this->_dbt->lastNameField."` varchar(20) NOT NULL,
		  `".$this->_dbt->passwordField."` varchar(65) NOT NULL,
		  `".$this->_dbt->emailField."` varchar(30) NOT NULL,
		  PRIMARY KEY (`".$this->_dbt->userIdField."`),
		  UNIQUE KEY `".$this->_dbt->userNameField."` (`".$this->_dbt->userNameField."`,`".$this->_dbt->emailField."`)
		) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
		
		$sqlUserType = "CREATE TABLE IF NOT EXISTS `".$this->_dbt->userTypeField."` (
		  `".$this->_dbt->userTypeIdField."` int(11) NOT NULL,
		  `".$this->_dbt->userTypeField."` varchar(20) NOT NULL DEFAULT 'public',
		  PRIMARY KEY (`".$this->_dbt->userTypeIdField."`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		
		$sqlRelationsUser = 
		"ALTER TABLE `".$this->_dbt->userTable."` ADD CONSTRAINT `".$this->_dbt->userTypeIdField."` FOREIGN KEY (`".$this->_dbt->userTypeIdField."`) REFERENCES `".$this->_dbt->userTypeField."` (`".$this->_dbt->userTypeIdField."`) ON DELETE NO ACTION ON UPDATE NO ACTION;";
		  
		$sqlRelationsAlbum = 
		"ALTER TABLE `".$this->_dbt->albumTable."` ADD CONSTRAINT `".$this->_dbt->userIdField."` FOREIGN KEY (`".$this->_dbt->userIdField."`) REFERENCES `".$this->_dbt->userTable."` (`".$this->_dbt->userIdField."`) ON DELETE CASCADE ON UPDATE NO ACTION;";
		
		$sqlRelationsImage = 
		"ALTER TABLE `".$this->_dbt->imageTable."` ADD CONSTRAINT `".$this->_dbt->albumIdField."` FOREIGN KEY (`".$this->_dbt->albumIdField."`) REFERENCES `".$this->_dbt->albumTable."` (`".$this->_dbt->albumIdField."`) ON DELETE CASCADE ON UPDATE NO ACTION;";
		
		$sqlRelationsToken = 
		"ALTER TABLE `".$this->_dbt->tokenField."` ADD CONSTRAINT `token_ibfk_1` FOREIGN KEY (`".$this->_dbt->userIdField."`) REFERENCES `".$this->_dbt->userTable."` (`".$this->_dbt->userIdField."`) ON DELETE CASCADE ON UPDATE NO ACTION;";

		$sqlInsertUserTypes = "INSERT INTO `".$this->_dbt->userTypeField."` (`".$this->_dbt->userTypeIdField."`, `".$this->_dbt->userTypeField."`) VALUES 
		(1, 'public'),
		(2, 'admin');";
		
		$sqlInsertUser = "INSERT INTO `".$this->_dbt->userTable."` (`".$this->_dbt->userTypeIdField."`, `".$this->_dbt->userNameField."`, `".$this->_dbt->firstNameField."`, `".$this->_dbt->lastNameField."`, `".$this->_dbt->passwordField."`, `".$this->_dbt->emailField."`) VALUES 
		(2, '".$userName."', '".$firstName."', '".$lastName."', '".$hashedPassword."', '".$email."');";
		
		$sqlInsertToken = "INSERT INTO `".$this->_dbt->tokenField."` (`".$this->_dbt->userIdField."`, `".$this->_dbt->tokenField."`, `".$this->_dbt->ipField."`) VALUES 
		(1, '508fcef93a83c', '0.0.0.0');";
		
		$queries = array(
		$sqlAlbum, 
		$sqlImage, 
		$sqlToken, 
		$sqlUser, 
		$sqlUserType, 
		$sqlRelationsUser, 
		$sqlRelationsAlbum, 
		$sqlRelationsImage,
		$sqlRelationsToken,
		$sqlInsertUserTypes,
		$sqlInsertUser,
		$sqlInsertToken);
		
		// Kör alla queries i ordningsföljd
		foreach ($queries as $query)
			$result = mysql_query($query) or die(mysql_error());

		mysql_close($connection);
		
		return true;
	}
}
?>